/** 
 * Description:文件上传类
 * Date:2011-11-20
 * Author:Along	
*/
package com.community.shopping.util;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileUpload {
	/**
	 * 文件上传
	 * @param request HttpServletRequest
	 * @param filedName 上传时页面表单的字段名称
	 * @param fileFolder 存放文件的文件夹名称
	 * @return 文件上传之后的文件路径全称
	 */
	public static String fileUpload(HttpServletRequest request,String filedName,String fileFolder){ 
		String systemBase=request.getSession().getServletContext().getRealPath("/");
		if(!fileFolder.startsWith(systemBase))
		{
		fileFolder=systemBase+fileFolder+"/";
		}
        String fileName="";
		MultipartHttpServletRequest   multipartRequest   =   (MultipartHttpServletRequest)   request; 
		CommonsMultipartFile  orginalFile =  (CommonsMultipartFile)multipartRequest.getFile(filedName);//表单中对应的文件名； 
		if (orginalFile!=null&&!orginalFile.isEmpty())   {//   如果有文章中带有附件 					
		//long fileSize=	orginalFile.getSize(); 暂不做大小验证
		String orginalFileName=orginalFile.getFileItem().getName();
		String extName=orginalFileName.substring(orginalFileName.lastIndexOf(".")+1);
		//新文件名
		fileName=String.valueOf(Calendar.getInstance().getTimeInMillis())+"."+extName.toLowerCase();
		DataOutputStream out=null;
		InputStream is=null;//   附件输入流 
		 try   
		 { 
			out = new   DataOutputStream(new  FileOutputStream(fileFolder+fileName));//存放文件的绝对路径 
		    is  = orginalFile.getInputStream(); 
		    byte[]   buffer   =   new   byte[1024]; 
		     while   (is.read(buffer)   >   0)   { 
	            out.write(buffer);//   写入磁盘； 
		        } 
		 }
		 catch(IOException   exception)
		 { 
		      exception.printStackTrace(); 
		 }
		 finally { 
			try{
		      if(is!=null)   { 
		          is.close(); 
		      } 
		      if(out!=null)   { 
		          out.close(); 
		      } 
			 }catch(Exception e){
				e.printStackTrace();
			   }
		     } 
		}
		//if(StringUtils.isNotBlank(fileName));
		  // Img_Middle.img_change(fileFolder, fileName);//生成缩略图
		return fileName;
	}
	
	/**
	 * 文件上传后直接压缩
	 * @param request HttpServletRequest
	 * @param filedName 上传时页面表单的字段名称
	 * @param fileFolder 存放文件的文件夹名称
	 * @return 文件上传之后的文件路径全称
	 */
	public static String fileUploadPress(HttpServletRequest request,String filedName,String fileFolder){ 
		String systemBase=request.getSession().getServletContext().getRealPath("/");
		if(!fileFolder.startsWith(systemBase))
		{
		fileFolder=systemBase+fileFolder+"/";
		}
        String fileName="";
		MultipartHttpServletRequest   multipartRequest   =   (MultipartHttpServletRequest)   request; 
		CommonsMultipartFile  orginalFile =  (CommonsMultipartFile)multipartRequest.getFile(filedName);//表单中对应的文件名； 
		if (orginalFile!=null&&!orginalFile.isEmpty())   {//   如果有文章中带有附件 					
		//long fileSize=	orginalFile.getSize(); 暂不做大小验证
		String orginalFileName=orginalFile.getFileItem().getName();
		String extName=orginalFileName.substring(orginalFileName.lastIndexOf(".")+1);
		//新文件名
		fileName=String.valueOf(Calendar.getInstance().getTimeInMillis())+"."+extName.toLowerCase();
		InputStream is=null;//   附件输入流 
		 try   
		 { 
		    is  = orginalFile.getInputStream(); 	
		   // Img_Middle.img_change(fileFolder, fileName,is);
		 }
		 catch(IOException   exception)
		 { 
		      exception.printStackTrace(); 
		 }
		 finally { 
			try{
		      if(is!=null)   { 
		          is.close(); 
		      } 
			 }catch(Exception e){
				e.printStackTrace();
			   }
		     } 
		}
		return fileName;
	}
	
	/**
	 * 文件上传
	 * @param request HttpServletRequest
	 * @param filedName 上传时页面表单的字段名称
	 * @param fileFolder 存放文件的文件夹名称
	 * @param newFileName 新文件的名称
	 * @return 文件上传之后的文件路径全称
	 */
	public static String fileUpload(HttpServletRequest request,String filedName,String fileFolder,String newFileName){ 
		String systemBase=request.getSession().getServletContext().getRealPath("/");
		if(!fileFolder.startsWith(systemBase))
		{
		fileFolder=systemBase+fileFolder+"/";
		}
        String fileName="";
		MultipartHttpServletRequest   multipartRequest   =   (MultipartHttpServletRequest)   request; 
		CommonsMultipartFile  orginalFile =  (CommonsMultipartFile)multipartRequest.getFile(filedName);//表单中对应的文件名； 
		if (orginalFile!=null&&!orginalFile.isEmpty())   {//   如果有文章中带有附件 					
		//long fileSize=	orginalFile.getSize(); 暂不做大小验证
		String orginalFileName=orginalFile.getFileItem().getName();
		String extName=orginalFileName.substring(orginalFileName.lastIndexOf(".")+1);
		//新文件名
		fileName=newFileName+"."+extName.toLowerCase();
		DataOutputStream out=null;
		InputStream is=null;//   附件输入流 
		 try   
		 { 
			out = new   DataOutputStream(new  FileOutputStream(fileFolder+fileName));//存放文件的绝对路径 
		    is  = orginalFile.getInputStream(); 
		    byte[]   buffer   =   new   byte[1024]; 
		     while   (is.read(buffer)   >   0)   { 
	            out.write(buffer);//   写入磁盘； 
		        } 
		 }
		 catch(IOException   exception)
		 { 
		      exception.printStackTrace(); 
		 }
		 finally { 
			try{
		      if(is!=null)   { 
		          is.close(); 
		      } 
		      if(out!=null)   { 
		          out.close(); 
		      } 
			 }catch(Exception e){
				e.printStackTrace();
			   }
		     } 
		}
		//if(StringUtils.isNotBlank(fileName));
		  // Img_Middle.img_change(fileFolder, fileName);//生成缩略图
		return fileName;
	}
}
